package it.free.maya;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

import org.apache.log4j.Logger;


/**
 * Load a properties file from a physical file
 * @author Stefano Falconetti
 */
public class SolrShardsPropertiesLoader {

	private static final String SOLRJ_EXTENSIONS_CONFIGURATION = "solrjextensions.properties";
	private static Logger logger = Logger.getLogger(SolrShardsPropertiesLoader.class);
	private static Properties properties = null;
	
	static{
		try{
			loadConfiguration();
		}catch(Exception exc) {
			   logger.info("Unable to load default configuration", exc);
		}
	}
	
    /**
     * Don't use
     */
    private SolrShardsPropertiesLoader() {
        super();
    }
    
    /**
     * @param fileName is the name of the file to load properties from
     * @return a properties object loaded, or null if something goes wrong
     * @throws IOException
     * @throws FileNotFoundException
     * @throws IllegalArgumentException
     * @throws SecurityException 
     */
    private static final Properties loadProperties(String fileName) throws IOException, FileNotFoundException, IllegalArgumentException, SecurityException {
        
        Properties properties = new Properties();
        FileInputStream fileInputStream= null;
        try{
            fileInputStream = new FileInputStream(fileName);
            properties.load(fileInputStream);
        }finally{
                 try{
                     if(fileInputStream != null){
                        fileInputStream.close();    
                     }
                 }catch(Exception exc){
                        throw (new IOException("Unable to close file input stream"));
                 }
        }
        return properties;
    }
     
    /**
     * Retrieve from the classpath of this classloader, the path for the jndi provider properties
     * @return the complete path for the file name passed, null if missing
     */
    private static final String getConfigFileFromClasspath(String fileName){
        
        URL configurationUrl = Thread.currentThread().getContextClassLoader().getResource(fileName);
        
        String absoluteConfigurationFile = null;
        if(configurationUrl == null){
           return absoluteConfigurationFile;
        }
        absoluteConfigurationFile = configurationUrl.getFile();
        return absoluteConfigurationFile;
    }
    
    /**
     * Load the default configuration file
     * @return the properties loaded
     */
    private static final void loadConfiguration() throws Exception {
    	    	
    	logger.info("Configuring solrj extensions");
        String configAbsolutePathName = SolrShardsPropertiesLoader.getConfigFileFromClasspath(SolrShardsPropertiesLoader.SOLRJ_EXTENSIONS_CONFIGURATION);
        
        logger.info("Using file: [" + configAbsolutePathName + "]");
        
        if(configAbsolutePathName == null){
           logger.error("Unable to locate in classpath resource [" + SolrShardsPropertiesLoader.SOLRJ_EXTENSIONS_CONFIGURATION + "], unable to proceed, please ensure that file is actually in classpath");
           return;
        }
    	
        SolrShardsPropertiesLoader.properties = SolrShardsPropertiesLoader.loadProperties(configAbsolutePathName);
    }
    
    /**
     * Read a property from its name
     * @param propertyName is the name of the property to read
     * @param defaultValue is the value returned if the property cannot be found
     * @return the value associated with that property name, if present 
     */
    public static final String getProperty(String propertyName, String defaultValue){
    	
    	String value = null;
    	
    	if(SolrShardsPropertiesLoader.properties != null){
    	   value = SolrShardsPropertiesLoader.properties.getProperty(propertyName, defaultValue);
    	}else{
    		  logger.error("Properties have not been loaded");
    		  value = defaultValue;
    	}
    	return value; 
    }
    
    /**
     * Read a property from its name
     * @param propertyName is the name of the property to read
     * @return the value associated with that property name, if present 
     */
    public static final String getProperty(String propertyName){
    	
    	String value = null;
    	
    	if(SolrShardsPropertiesLoader.properties != null){
    	   value = SolrShardsPropertiesLoader.properties.getProperty(propertyName, null);
    	}else{
    		  logger.error("Properties have not been loaded");
    	}
    	return value; 
    }
    
}
